package gui.data.D3;

import static util.Util.checkNumeric;
import gui.ErrorMessage;
import gui.MyFrame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import util.NumericCheckWrapper;

import calcs.CircleCalculator;
import calcs.SphereCalculator;

public class SphereInputGui extends JPanel {

	JLabel areaLbl, volumeLbl, circumferenceLbl, radiusLbl, capVolumeLbl,
			capAreaLbl, capCircumferenceLbl, capRadiusLbl, capHeightLbl;
	JTextField areaInput, volumeInput, circumferenceInput, radiusInput,
			capVolumeInput, capAreaInput, capCircumferenceInput,
			capRadiusInput, capHeightInput;
	MyFrame myFrame;

	public SphereInputGui(MyFrame myFrame) {
		this.myFrame = myFrame;
		this.setLayout(new GridLayout(10, 2));
		this.setPreferredSize(new Dimension(500, 500));
		volumeLbl = new JLabel("Volume = ");
		circumferenceLbl = new JLabel("Circumference = ");
		capAreaLbl = new JLabel("Cap area = ");
		areaLbl = new JLabel("Area = ");
		capHeightLbl = new JLabel("Cap height = ");
		radiusLbl = new JLabel("Radius = ");
		capRadiusLbl = new JLabel("Cap radius = ");
		capCircumferenceLbl = new JLabel("Cap circumference = ");
		capVolumeLbl = new JLabel("Cap Volume = ");
		volumeInput = new JTextField();
		circumferenceInput = new JTextField();
		capAreaInput = new JTextField();
		areaInput = new JTextField();
		capHeightInput = new JTextField();
		radiusInput = new JTextField();
		capRadiusInput = new JTextField();
		capCircumferenceInput = new JTextField();
		capVolumeInput = new JTextField();
		this.add(radiusLbl);
		this.add(radiusInput);
		this.add(circumferenceLbl);
		this.add(circumferenceInput);
		this.add(areaLbl);
		this.add(areaInput);
		this.add(volumeLbl);
		this.add(volumeInput);
		this.add(capRadiusLbl);
		this.add(capRadiusInput);
		this.add(capHeightLbl);
		this.add(capHeightInput);
		this.add(capCircumferenceLbl);
		this.add(capCircumferenceInput);
		this.add(capAreaLbl);
		this.add(capAreaInput);
		this.add(capVolumeLbl);
		this.add(capVolumeInput);
		JButton calculate = new JButton("Calculate");
		calculate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				openResult();
			}
		});
		this.add(new JLabel());
		this.add(calculate);
	}

	private void openResult() {
		
		boolean error = false;
		JTextField[] textFields = {volumeInput, areaInput, circumferenceInput, radiusInput, capVolumeInput, capAreaInput, capCircumferenceInput, capRadiusInput, capHeightInput};
		Double[] values = new Double[9];
		for(int i = 0; i<textFields.length; i++){
			NumericCheckWrapper res = checkNumeric(textFields[i].getText());
			if(!res.isCorrect()){
				error = true;
				new ErrorMessage("Check input!");
				break;
			}else{
				values[i] = res.getValue();
			}
		}
		
		if(error){
			error = false;
		}else{
			new SphereCalculator(values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], myFrame);
		}
	}
}
